//-----------------------------------------------------------------------
// <copyright file="ExtractFile.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Extracts the file from the ZipFile specified.</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Zip
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Extracts the requested file from the specified zip file using J# 2.0.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Zip.Extract PathToZipFile="pathToZipFile" PathToFile="pathToFile" FileToExtract="fileToExtract" />]]></code>
    /// <para>where:</para>
    /// <para><i>pathToZipFile (Required)</i></para>
    /// <para>The path to the zip file to be extracted from.</para>
    /// <para><i>pathToFile (Required)</i></para>
    /// <para>The path to where files should be extracted to.</para>
    /// <para><i>fileToExtract</i></para>
    /// <para>The file to extract from the zip file.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Zip.Extract
    ///             PathToZipFile="c:\newzip.zip" 
    ///             PathToFile="c:\extractedfiles"/
    ///             FileToExtract="extractme.txt">
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class ExtractFile : TaskBase
    {
        private string fileToExtract = String.Empty;
        private string pathToFile = String.Empty;
        private string pathToZipFile = String.Empty;
        private bool skipExisting;

        /// <summary>
        /// Path to where files should be extracted
        /// </summary>
        /// <value>Path</value>
        [Required]
        public string PathToFile
        {
            get { return this.pathToFile ?? String.Empty; }
            set { this.pathToFile = value; }
        }

        /// <summary>
        /// Set to true in order to skip over files that already exist with the same or newer date
        /// </summary>
        /// <value>defaults to true</value>
        public bool SkipExisting
        {
            get { return this.skipExisting; }
            set { this.skipExisting = value; }
        }

        /// <summary>
        /// Specifies the path to and filename of the zip file the add action is to be performed on.
        /// </summary>
        /// <value>The path to and filename of the zip file.</value>
        [Required]
        public string PathToZipFile
        {
            get { return this.pathToZipFile ?? String.Empty; }
            set { this.pathToZipFile = value; }
        }

        /// <summary>
        /// Specific filename to extract from the zip file
        /// </summary>
        /// <value>File name to extract</value>
        public string FileToExtract
        {
            get { return this.fileToExtract ?? String.Empty; }
            set { this.fileToExtract = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.fileToExtract.Length == 0)
            {
                this.Log.LogMessageFromResources("Zip.Extract", this.pathToZipFile, this.pathToFile);
                ZipManager.Extract(this.pathToZipFile, this.pathToFile, this.skipExisting);
            }
            else
            {
                this.Log.LogMessageFromResources("Zip.ExtractFile", this.fileToExtract, this.pathToZipFile, this.pathToFile);
                ZipManager.Extract(this.pathToZipFile, this.pathToFile, this.fileToExtract, this.skipExisting);
            }
        }
    }
}